#!/usr/bin/env python3
#coding: utf-8
import re

xtable = {
    b"www.amazon.cn" : re.compile(b"dp/[A-Z0-9]+"),
    b"www.escentual.com" : re.compile(b".+"),
    b"www.luisaviaroma.com" : re.compile(b"index.aspx.*"),
    b"www.wiggle.cn" :  re.compile(b".+"),
    b"www.jomashop.com" : re.compile(b"\\.html"),
    b"www.amazon.ca" : re.compile(b"product/[A-Z0-9]+"),
    b"www.6pm.com" : re.compile(b"product/\\d+"),
    b"www1.macys.com" : re.compile(b"product/.+"),
    b"www.finishline.com" : re.compile(b"product\\?.+"),
    b"www.amazon.co.uk" : re.compile(b"(?:dp|product)/[A-Z0-9]+")
}

def urlsplit(url):
    if url.startswith(b"http://"):
        return url[7:].split(b'/', 1)
    else:
        return url.split(b'/', 1)

def match(url):
    host, path = urlsplit(url)
    return xtable[host].search(path)

def test():
    urls = [b'http://www.amazon.cn/dp/B00UKPMA44/ref=gwgfloorv1_CE_a_0?pf_rd_p=267224552&pf_rd_s=desktop-6',
            b'http://www.escentual.com/hair-care/bumblefullpotential004/',
            b'http://www.luisaviaroma.com/index.aspx?#ItemSrv.ashx|SeasonId=63I&CollectionId=G85&ItemId=17',
            b'http://www.wiggle.cn/eastway-emitter-r4-tiagra-2016/',
            b'http://www.jomashop.com/ferragamo-8050445769040.html',
            b'http://www.amazon.ca/gp/product/B00EAKKOKC/ref=br_bsl_pdt-3/179-1951858-8163852?pf_rd_m=A3DWYIK6Y9EEQB',
            b'http://www.6pm.com/product/8642433/color/518509',
            b'http://www1.macys.com/shop/product/scalamandre-tropez-4-piece-hostess-set?ID=1137462&CategoryID=7919',
            b'http://www.finishline.com/store/product?A=5587&categoryId=cat303193&productId=prod781512',
            b'http://www.amazon.co.uk/gp/product/B013F9YDZU?ref_=gbps_img_s-3_3127_619facb5&smid=A3P5ROKL5A1OLE']

    for i in map(match, urls):
        print(i)

    for i in range(1000000):
        for u in urls:
            match(u)
test()



